/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

//package client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 *
 * @author josegarcia
 */
public class MainClient {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {

        Socket kkSocket = null;
        PrintWriter out = null;
        BufferedReader in = null;

        try {
            kkSocket = new Socket("localhost", 2222);
            out = new PrintWriter(kkSocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
        } catch (UnknownHostException e) {
            System.err.println("Cannot find host");
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for host.");
            System.exit(1);
        }

        BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
        String serverInput;
        String userInput=null;

	while(userInput==null){
	    userInput = stdIn.readLine();
		if (userInput != null&& !userInput.equals("QUIT")) {
		    out.println(userInput);
	        }
	}
        while ((serverInput = in.readLine()) != null&&!userInput.equals("QUIT")) {
            System.out.println("Server Response: " + serverInput);
            if (serverInput.equals("EXIT"))
                break;

            userInput = stdIn.readLine();
	    if (userInput != null) {
                out.println(userInput);
	    }
        }

        out.close();
        in.close();
        stdIn.close();
        kkSocket.close();
    }

}
